{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "from openfermionpyscf import run_pyscf\n",
    "from openfermion.transforms import binary_code_transform, bravyi_kitaev_code, get_fermion_operator\n",
    "from openfermion.hamiltonians import MolecularData\n",
    "from openfermion.ops import FermionOperator, QubitOperator\n",
    "from openfermion.utils import count_qubits\n",
    "from pyscf import gto, scf, mcscf\n",
    "\n",
    "from helper_functions import *\n",
    "from XBK_method import *\n",
    "from QCC_method import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#create molecule\n",
    "name = 'H3'\n",
    "charge = 1\n",
    "multiplicity = 1\n",
    "basis = 'sto-6g'\n",
    "\n",
    "bond_length = 1.1\n",
    "geometry = get_molGeometry(name, bond_length)\n",
    "    \n",
    "molecule = MolecularData(\n",
    "    geometry=geometry,\n",
    "    basis=basis,\n",
    "    multiplicity=multiplicity,\n",
    "    charge=charge\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1.240069304526973\n"
     ]
    }
   ],
   "source": [
    "#run RHF calculations\n",
    "molecule = run_pyscf(molecule, run_scf=True)\n",
    "hf_energy = float(molecule.hf_energy)\n",
    "hf_data = molecule._pyscf_data['scf']\n",
    "\n",
    "print(hf_energy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1.2722647705358225\n"
     ]
    }
   ],
   "source": [
    "#define active space\n",
    "n_active_electrons = 2\n",
    "n_active_orbitals = 3\n",
    "occupied_indices, active_indices = get_active_space(molecule, n_active_electrons, n_active_orbitals)\n",
    "\n",
    "#run CASCI calculations\n",
    "casci_data = hf_data.CASCI(n_active_orbitals, n_active_electrons).run(verbose=False)\n",
    "casci_energy = float(casci_data.e_tot)\n",
    "\n",
    "print(casci_energy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21.443210575236364 [] +\n",
      "-21.623075395384372 [0^ 0] +\n",
      "5.0 [0^ 0 0^ 0] +\n",
      "5.0 [0^ 0 1^ 1] +\n",
      "5.0 [0^ 0 2^ 2] +\n",
      "5.0 [0^ 0 3^ 3] +\n",
      "5.0 [0^ 0 4^ 4] +\n",
      "5.0 [0^ 0 5^ 5] +\n",
      "0.28143545550270416 [0^ 0^ 0 0] +\n",
      "0.07258994740907981 [0^ 0^ 2 2] +\n",
      "0.0725899474090799 [0^ 0^ 4 4] +\n",
      "0.28143545550270416 [0^ 1^ 1 0] +\n",
      "0.07258994740907981 [0^ 1^ 3 2] +\n",
      "0.0725899474090799 [0^ 1^ 5 4] +\n",
      "0.07258994740907981 [0^ 2^ 0 2] +\n",
      "0.27480121211888664 [0^ 2^ 2 0] +\n",
      "0.03859585573396527 [0^ 2^ 2 2] +\n",
      "-0.03156146519041141 [0^ 2^ 2 4] +\n",
      "-0.031561465190411364 [0^ 2^ 4 2] +\n",
      "-0.038595855733965366 [0^ 2^ 4 4] +\n",
      "0.07258994740907981 [0^ 3^ 1 2] +\n",
      "0.27480121211888664 [0^ 3^ 3 0] +\n",
      "0.03859585573396527 [0^ 3^ 3 2] +\n",
      "-0.03156146519041141 [0^ 3^ 3 4] +\n",
      "-0.031561465190411364 [0^ 3^ 5 2] +\n",
      "-0.038595855733965366 [0^ 3^ 5 4] +\n",
      "0.0725899474090799 [0^ 4^ 0 4] +\n",
      "-0.031561465190411364 [0^ 4^ 2 2] +\n",
      "-0.038595855733965366 [0^ 4^ 2 4] +\n",
      "0.2748012121188869 [0^ 4^ 4 0] +\n",
      "-0.03859585573396542 [0^ 4^ 4 2] +\n",
      "0.031561465190411385 [0^ 4^ 4 4] +\n",
      "0.0725899474090799 [0^ 5^ 1 4] +\n",
      "-0.031561465190411364 [0^ 5^ 3 2] +\n",
      "-0.038595855733965366 [0^ 5^ 3 4] +\n",
      "0.2748012121188869 [0^ 5^ 5 0] +\n",
      "-0.03859585573396542 [0^ 5^ 5 2] +\n",
      "0.031561465190411385 [0^ 5^ 5 4] +\n",
      "0.28143545550270416 [1^ 0^ 0 1] +\n",
      "0.07258994740907981 [1^ 0^ 2 3] +\n",
      "0.0725899474090799 [1^ 0^ 4 5] +\n",
      "-21.623075395384372 [1^ 1] +\n",
      "5.0 [1^ 1 0^ 0] +\n",
      "5.0 [1^ 1 1^ 1] +\n",
      "5.0 [1^ 1 2^ 2] +\n",
      "5.0 [1^ 1 3^ 3] +\n",
      "5.0 [1^ 1 4^ 4] +\n",
      "5.0 [1^ 1 5^ 5] +\n",
      "0.28143545550270416 [1^ 1^ 1 1] +\n",
      "0.07258994740907981 [1^ 1^ 3 3] +\n",
      "0.0725899474090799 [1^ 1^ 5 5] +\n",
      "0.07258994740907981 [1^ 2^ 0 3] +\n",
      "0.27480121211888664 [1^ 2^ 2 1] +\n",
      "0.03859585573396527 [1^ 2^ 2 3] +\n",
      "-0.03156146519041141 [1^ 2^ 2 5] +\n",
      "-0.031561465190411364 [1^ 2^ 4 3] +\n",
      "-0.038595855733965366 [1^ 2^ 4 5] +\n",
      "0.07258994740907981 [1^ 3^ 1 3] +\n",
      "0.27480121211888664 [1^ 3^ 3 1] +\n",
      "0.03859585573396527 [1^ 3^ 3 3] +\n",
      "-0.03156146519041141 [1^ 3^ 3 5] +\n",
      "-0.031561465190411364 [1^ 3^ 5 3] +\n",
      "-0.038595855733965366 [1^ 3^ 5 5] +\n",
      "0.0725899474090799 [1^ 4^ 0 5] +\n",
      "-0.031561465190411364 [1^ 4^ 2 3] +\n",
      "-0.038595855733965366 [1^ 4^ 2 5] +\n",
      "0.2748012121188869 [1^ 4^ 4 1] +\n",
      "-0.03859585573396542 [1^ 4^ 4 3] +\n",
      "0.031561465190411385 [1^ 4^ 4 5] +\n",
      "0.0725899474090799 [1^ 5^ 1 5] +\n",
      "-0.031561465190411364 [1^ 5^ 3 3] +\n",
      "-0.038595855733965366 [1^ 5^ 3 5] +\n",
      "0.2748012121188869 [1^ 5^ 5 1] +\n",
      "-0.03859585573396542 [1^ 5^ 5 3] +\n",
      "0.031561465190411385 [1^ 5^ 5 5] +\n",
      "0.27480121211888714 [2^ 0^ 0 2] +\n",
      "0.07258994740907981 [2^ 0^ 2 0] +\n",
      "0.038595855733965394 [2^ 0^ 2 2] +\n",
      "-0.0315614651904115 [2^ 0^ 2 4] +\n",
      "-0.031561465190411545 [2^ 0^ 4 2] +\n",
      "-0.038595855733965526 [2^ 0^ 4 4] +\n",
      "0.27480121211888714 [2^ 1^ 1 2] +\n",
      "0.07258994740907981 [2^ 1^ 3 0] +\n",
      "0.038595855733965394 [2^ 1^ 3 2] +\n",
      "-0.0315614651904115 [2^ 1^ 3 4] +\n",
      "-0.031561465190411545 [2^ 1^ 5 2] +\n",
      "-0.038595855733965526 [2^ 1^ 5 4] +\n",
      "-21.061278062380566 [2^ 2] +\n",
      "5.0 [2^ 2 0^ 0] +\n",
      "5.0 [2^ 2 1^ 1] +\n",
      "5.0 [2^ 2 2^ 2] +\n",
      "5.0 [2^ 2 3^ 3] +\n",
      "5.0 [2^ 2 4^ 4] +\n",
      "5.0 [2^ 2 5^ 5] +\n",
      "0.07258994740907981 [2^ 2^ 0 0] +\n",
      "0.038595855733965394 [2^ 2^ 0 2] +\n",
      "-0.0315614651904115 [2^ 2^ 0 4] +\n",
      "0.03859585573396527 [2^ 2^ 2 0] +\n",
      "0.31887681418620134 [2^ 2^ 2 2] +\n",
      "-0.031561465190411364 [2^ 2^ 4 0] +\n",
      "0.03932724296935464 [2^ 2^ 4 4] +\n",
      "0.07258994740907981 [2^ 3^ 1 0] +\n",
      "0.038595855733965394 [2^ 3^ 1 2] +\n",
      "-0.0315614651904115 [2^ 3^ 1 4] +\n",
      "0.03859585573396527 [2^ 3^ 3 0] +\n",
      "0.31887681418620134 [2^ 3^ 3 2] +\n",
      "-0.031561465190411364 [2^ 3^ 5 0] +\n",
      "0.03932724296935464 [2^ 3^ 5 4] +\n",
      "-0.031561465190411545 [2^ 4^ 0 2] +\n",
      "-0.038595855733965526 [2^ 4^ 0 4] +\n",
      "-0.031561465190411364 [2^ 4^ 2 0] +\n",
      "0.03932724296935464 [2^ 4^ 2 4] +\n",
      "-0.03859585573396542 [2^ 4^ 4 0] +\n",
      "0.240222328247492 [2^ 4^ 4 2] +\n",
      "-0.031561465190411545 [2^ 5^ 1 2] +\n",
      "-0.038595855733965526 [2^ 5^ 1 4] +\n",
      "-0.031561465190411364 [2^ 5^ 3 0] +\n",
      "0.03932724296935464 [2^ 5^ 3 4] +\n",
      "-0.03859585573396542 [2^ 5^ 5 0] +\n",
      "0.240222328247492 [2^ 5^ 5 2] +\n",
      "0.27480121211888714 [3^ 0^ 0 3] +\n",
      "0.07258994740907981 [3^ 0^ 2 1] +\n",
      "0.038595855733965394 [3^ 0^ 2 3] +\n",
      "-0.0315614651904115 [3^ 0^ 2 5] +\n",
      "-0.031561465190411545 [3^ 0^ 4 3] +\n",
      "-0.038595855733965526 [3^ 0^ 4 5] +\n",
      "0.27480121211888714 [3^ 1^ 1 3] +\n",
      "0.07258994740907981 [3^ 1^ 3 1] +\n",
      "0.038595855733965394 [3^ 1^ 3 3] +\n",
      "-0.0315614651904115 [3^ 1^ 3 5] +\n",
      "-0.031561465190411545 [3^ 1^ 5 3] +\n",
      "-0.038595855733965526 [3^ 1^ 5 5] +\n",
      "0.07258994740907981 [3^ 2^ 0 1] +\n",
      "0.038595855733965394 [3^ 2^ 0 3] +\n",
      "-0.0315614651904115 [3^ 2^ 0 5] +\n",
      "0.03859585573396527 [3^ 2^ 2 1] +\n",
      "0.31887681418620134 [3^ 2^ 2 3] +\n",
      "-0.031561465190411364 [3^ 2^ 4 1] +\n",
      "0.03932724296935464 [3^ 2^ 4 5] +\n",
      "-21.061278062380566 [3^ 3] +\n",
      "5.0 [3^ 3 0^ 0] +\n",
      "5.0 [3^ 3 1^ 1] +\n",
      "5.0 [3^ 3 2^ 2] +\n",
      "5.0 [3^ 3 3^ 3] +\n",
      "5.0 [3^ 3 4^ 4] +\n",
      "5.0 [3^ 3 5^ 5] +\n",
      "0.07258994740907981 [3^ 3^ 1 1] +\n",
      "0.038595855733965394 [3^ 3^ 1 3] +\n",
      "-0.0315614651904115 [3^ 3^ 1 5] +\n",
      "0.03859585573396527 [3^ 3^ 3 1] +\n",
      "0.31887681418620134 [3^ 3^ 3 3] +\n",
      "-0.031561465190411364 [3^ 3^ 5 1] +\n",
      "0.03932724296935464 [3^ 3^ 5 5] +\n",
      "-0.031561465190411545 [3^ 4^ 0 3] +\n",
      "-0.038595855733965526 [3^ 4^ 0 5] +\n",
      "-0.031561465190411364 [3^ 4^ 2 1] +\n",
      "0.03932724296935464 [3^ 4^ 2 5] +\n",
      "-0.03859585573396542 [3^ 4^ 4 1] +\n",
      "0.240222328247492 [3^ 4^ 4 3] +\n",
      "-0.031561465190411545 [3^ 5^ 1 3] +\n",
      "-0.038595855733965526 [3^ 5^ 1 5] +\n",
      "-0.031561465190411364 [3^ 5^ 3 1] +\n",
      "0.03932724296935464 [3^ 5^ 3 5] +\n",
      "-0.03859585573396542 [3^ 5^ 5 1] +\n",
      "0.240222328247492 [3^ 5^ 5 3] +\n",
      "0.2748012121188873 [4^ 0^ 0 4] +\n",
      "-0.0315614651904115 [4^ 0^ 2 2] +\n",
      "-0.038595855733965505 [4^ 0^ 2 4] +\n",
      "0.0725899474090799 [4^ 0^ 4 0] +\n",
      "-0.038595855733965526 [4^ 0^ 4 2] +\n",
      "0.031561465190411434 [4^ 0^ 4 4] +\n",
      "0.2748012121188873 [4^ 1^ 1 4] +\n",
      "-0.0315614651904115 [4^ 1^ 3 2] +\n",
      "-0.038595855733965505 [4^ 1^ 3 4] +\n",
      "0.0725899474090799 [4^ 1^ 5 0] +\n",
      "-0.038595855733965526 [4^ 1^ 5 2] +\n",
      "0.031561465190411434 [4^ 1^ 5 4] +\n",
      "-0.0315614651904115 [4^ 2^ 0 2] +\n",
      "-0.038595855733965505 [4^ 2^ 0 4] +\n",
      "-0.03156146519041141 [4^ 2^ 2 0] +\n",
      "0.2402223282474919 [4^ 2^ 2 4] +\n",
      "-0.038595855733965366 [4^ 2^ 4 0] +\n",
      "0.03932724296935464 [4^ 2^ 4 2] +\n",
      "-0.0315614651904115 [4^ 3^ 1 2] +\n",
      "-0.038595855733965505 [4^ 3^ 1 4] +\n",
      "-0.03156146519041141 [4^ 3^ 3 0] +\n",
      "0.2402223282474919 [4^ 3^ 3 4] +\n",
      "-0.038595855733965366 [4^ 3^ 5 0] +\n",
      "0.03932724296935464 [4^ 3^ 5 2] +\n",
      "-21.061278062380566 [4^ 4] +\n",
      "5.0 [4^ 4 0^ 0] +\n",
      "5.0 [4^ 4 1^ 1] +\n",
      "5.0 [4^ 4 2^ 2] +\n",
      "5.0 [4^ 4 3^ 3] +\n",
      "5.0 [4^ 4 4^ 4] +\n",
      "5.0 [4^ 4 5^ 5] +\n",
      "0.0725899474090799 [4^ 4^ 0 0] +\n",
      "-0.038595855733965526 [4^ 4^ 0 2] +\n",
      "0.031561465190411434 [4^ 4^ 0 4] +\n",
      "-0.038595855733965366 [4^ 4^ 2 0] +\n",
      "0.03932724296935464 [4^ 4^ 2 2] +\n",
      "0.031561465190411385 [4^ 4^ 4 0] +\n",
      "0.31887681418620195 [4^ 4^ 4 4] +\n",
      "0.0725899474090799 [4^ 5^ 1 0] +\n",
      "-0.038595855733965526 [4^ 5^ 1 2] +\n",
      "0.031561465190411434 [4^ 5^ 1 4] +\n",
      "-0.038595855733965366 [4^ 5^ 3 0] +\n",
      "0.03932724296935464 [4^ 5^ 3 2] +\n",
      "0.031561465190411385 [4^ 5^ 5 0] +\n",
      "0.31887681418620195 [4^ 5^ 5 4] +\n",
      "0.2748012121188873 [5^ 0^ 0 5] +\n",
      "-0.0315614651904115 [5^ 0^ 2 3] +\n",
      "-0.038595855733965505 [5^ 0^ 2 5] +\n",
      "0.0725899474090799 [5^ 0^ 4 1] +\n",
      "-0.038595855733965526 [5^ 0^ 4 3] +\n",
      "0.031561465190411434 [5^ 0^ 4 5] +\n",
      "0.2748012121188873 [5^ 1^ 1 5] +\n",
      "-0.0315614651904115 [5^ 1^ 3 3] +\n",
      "-0.038595855733965505 [5^ 1^ 3 5] +\n",
      "0.0725899474090799 [5^ 1^ 5 1] +\n",
      "-0.038595855733965526 [5^ 1^ 5 3] +\n",
      "0.031561465190411434 [5^ 1^ 5 5] +\n",
      "-0.0315614651904115 [5^ 2^ 0 3] +\n",
      "-0.038595855733965505 [5^ 2^ 0 5] +\n",
      "-0.03156146519041141 [5^ 2^ 2 1] +\n",
      "0.2402223282474919 [5^ 2^ 2 5] +\n",
      "-0.038595855733965366 [5^ 2^ 4 1] +\n",
      "0.03932724296935464 [5^ 2^ 4 3] +\n",
      "-0.0315614651904115 [5^ 3^ 1 3] +\n",
      "-0.038595855733965505 [5^ 3^ 1 5] +\n",
      "-0.03156146519041141 [5^ 3^ 3 1] +\n",
      "0.2402223282474919 [5^ 3^ 3 5] +\n",
      "-0.038595855733965366 [5^ 3^ 5 1] +\n",
      "0.03932724296935464 [5^ 3^ 5 3] +\n",
      "0.0725899474090799 [5^ 4^ 0 1] +\n",
      "-0.038595855733965526 [5^ 4^ 0 3] +\n",
      "0.031561465190411434 [5^ 4^ 0 5] +\n",
      "-0.038595855733965366 [5^ 4^ 2 1] +\n",
      "0.03932724296935464 [5^ 4^ 2 3] +\n",
      "0.031561465190411385 [5^ 4^ 4 1] +\n",
      "0.31887681418620195 [5^ 4^ 4 5] +\n",
      "-21.061278062380566 [5^ 5] +\n",
      "5.0 [5^ 5 0^ 0] +\n",
      "5.0 [5^ 5 1^ 1] +\n",
      "5.0 [5^ 5 2^ 2] +\n",
      "5.0 [5^ 5 3^ 3] +\n",
      "5.0 [5^ 5 4^ 4] +\n",
      "5.0 [5^ 5 5^ 5] +\n",
      "0.0725899474090799 [5^ 5^ 1 1] +\n",
      "-0.038595855733965526 [5^ 5^ 1 3] +\n",
      "0.031561465190411434 [5^ 5^ 1 5] +\n",
      "-0.038595855733965366 [5^ 5^ 3 1] +\n",
      "0.03932724296935464 [5^ 5^ 3 3] +\n",
      "0.031561465190411385 [5^ 5^ 5 1] +\n",
      "0.31887681418620195 [5^ 5^ 5 5]\n"
     ]
    }
   ],
   "source": [
    "#convert to fermionic Hamiltonian\n",
    "molecular_H = molecule.get_molecular_hamiltonian(occupied_indices=occupied_indices, active_indices=active_indices)\n",
    "if molecular_H[()] == None:\n",
    "    molecular_H[()] = 0\n",
    "fermionic_H = get_fermion_operator(molecular_H)\n",
    "\n",
    "#add penalty term to ensure correct number of electrons in ground state\n",
    "weight = 5\n",
    "penalty_term = FermionOperator('', n_active_electrons)\n",
    "\n",
    "for i in range(molecular_H.n_qubits):\n",
    "    penalty_term += FermionOperator(str(i)+'^ '+str(i), -1)\n",
    "fermionic_H += weight*penalty_term**2\n",
    "\n",
    "print(fermionic_H)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1.2722647705358519 \n",
      "\n",
      "12.052315964211402 [] +\n",
      "-0.019297927866982666 [X0] +\n",
      "0.019297927866982718 [X0 X1 X2] +\n",
      "-0.015780732595205717 [X0 X1 Z2 X3] +\n",
      "0.019297927866982666 [X0 X1 Z2 Z3] +\n",
      "-0.015780732595205717 [X0 X1 X3] +\n",
      "0.019297927866982666 [X0 X1 Z3] +\n",
      "0.019297927866982718 [X0 Y1 Y2] +\n",
      "0.015780732595205703 [X0 Z1 X2 X3] +\n",
      "-0.01929792786698272 [X0 Z1 X2 Z3] +\n",
      "0.01929792786698273 [X0 Z1 Z2] +\n",
      "-0.015780732595205703 [X0 Z1 X3] +\n",
      "0.01929792786698273 [X0 Z1 Z3] +\n",
      "0.015780732595205703 [X0 X2 X3] +\n",
      "-0.01929792786698272 [X0 X2 Z3] +\n",
      "0.015780732595205738 [X0 Z2 X3] +\n",
      "-0.019297927866982666 [X0 Z2 Z3] +\n",
      "-0.015780732595205717 [Y0 X1 X2 Y3] +\n",
      "-0.015780732595205717 [Y0 X1 Y2 X3] +\n",
      "0.019297927866982718 [Y0 X1 Y2 Z3] +\n",
      "0.015780732595205717 [Y0 Y1 X2 X3] +\n",
      "-0.019297927866982718 [Y0 Y1 X2 Z3] +\n",
      "-0.015780732595205717 [Y0 Y1 Y2 Y3] +\n",
      "-0.015780732595205717 [Y0 Y1 Z2 X3] +\n",
      "0.01929792786698273 [Y0 Y1 Z2 Z3] +\n",
      "-0.015780732595205717 [Y0 Y1 X3] +\n",
      "0.01929792786698273 [Y0 Y1 Z3] +\n",
      "0.015780732595205738 [Y0 Z1 Y2 X3] +\n",
      "-0.01929792786698272 [Y0 Z1 Y2 Z3] +\n",
      "-0.015780732595205738 [Y0 Z1 Z2 Y3] +\n",
      "0.015780732595205738 [Y0 Y2 X3] +\n",
      "-0.01929792786698272 [Y0 Y2 Z3] +\n",
      "0.015780732595205703 [Y0 Y3] +\n",
      "2.640717727751352 [Z0] +\n",
      "0.03629497370453991 [Z0 X1] +\n",
      "0.01966362148467732 [Z0 X1 X2 Z3] +\n",
      "0.03629497370453991 [Z0 X1 Z2] +\n",
      "0.03629497370453991 [Z0 X1 Z2 Z3] +\n",
      "0.03629497370453991 [Z0 X1 Z3] +\n",
      "0.01966362148467732 [Z0 Y1 Y2 Z3] +\n",
      "-2.6374006060594435 [Z0 Z1] +\n",
      "0.03629497370453994 [Z0 Z1 X2] +\n",
      "7.406640049526931 [Z0 Z1 Z2] +\n",
      "2.6201111641237462 [Z0 Z1 Z2 Z3] +\n",
      "-7.688969953434883 [Z0 Z1 Z3] +\n",
      "0.03629497370453994 [Z0 X2] +\n",
      "-2.6374006060594435 [Z0 Z2] +\n",
      "-2.6374006060594435 [Z0 Z2 Z3] +\n",
      "2.6594384070931008 [Z0 Z3] +\n",
      "0.01966362148467732 [X1 X2 Z3] +\n",
      "0.01966362148467732 [Y1 Y2 Z3] +\n",
      "-7.688969953434883 [Z1] +\n",
      "0.03629497370453994 [Z1 X2 Z3] +\n",
      "7.406640049526931 [Z1 Z2] +\n",
      "2.6201111641237462 [Z1 Z2 Z3] +\n",
      "-2.6374006060594435 [Z1 Z3] +\n",
      "0.03629497370453994 [X2 Z3] +\n",
      "-7.688969953434883 [Z2] +\n",
      "-7.688969953434883 [Z2 Z3] +\n",
      "2.659438407093101 [Z3]\n"
     ]
    }
   ],
   "source": [
    "#convert to Pauli operator Hamiltonian\n",
    "binary_code = bravyi_kitaev_code(molecular_H.n_qubits)\n",
    "qubit_H = binary_code_transform(fermionic_H, binary_code)\n",
    "qubit_H.compress()\n",
    "\n",
    "#apply symmetry reductions and calculate minimum eigenvalue (should be equal to CASCI energy)\n",
    "sectors = taper_qubits(qubit_H)\n",
    "qubit_H, min_eigenvalue = sector_with_ground(sectors)\n",
    "m = count_qubits(qubit_H)\n",
    "\n",
    "print(min_eigenvalue, '\\n')\n",
    "print(qubit_H)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#set sampler to perform the annealing\n",
    "from neal import SimulatedAnnealingSampler\n",
    "sampler = SimulatedAnnealingSampler() #uses simulated annealing, see D-Wave's ocean sdk for more options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1.24006930460746\n",
      "[1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "### XBK method ###\n",
    "\n",
    "#set r value\n",
    "r = 4\n",
    "\n",
    "#construct qubit Hamiltonians and C terms for XBK method\n",
    "qubit_Hs, qubit_Cs = [],[]\n",
    "for p in range(int(math.ceil(r/2+1))):\n",
    "    qubit_Hs += [XBK_transform(qubit_H, r, p)]\n",
    "    qubit_Cs += [construct_C(m, r, p)]\n",
    "\n",
    "#run XBK method\n",
    "XBK_energy, ground_state = XBK(qubit_Hs, qubit_Cs, r, sampler, starting_lam=0, num_samples=1000, strength=1e3, verbose=False)\n",
    "\n",
    "print(XBK_energy)\n",
    "print(ground_state) #ground state in rm-qubit space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1.2664583599980688\n",
      "{phi0: 1.3564044038755205, phi1: 6.283185307179586, phi2: 3.5834420620660397, phi3: 3.1465829731980053, the0: 4.554931150993846e-06, the1: 3.141592653589793, the2: 2.827524175524541e-07, the3: 0.0, tau0: 2.9829102234933917, tau1: 6.12449846615571, tau2: 6.283185307179586, tau3: 0.0}\n"
     ]
    }
   ],
   "source": [
    "### QCC method ###\n",
    "\n",
    "#set number of Bloch angle and entangler amplitude foldings\n",
    "angle_folds = 2\n",
    "amplitude_folds = 1\n",
    "\n",
    "#create dictionary of QubitOperator entanglers\n",
    "entanglers = {'IYZI': QubitOperator('Y1 Z2'), 'IZYI': QubitOperator('Z1 Y2'),\n",
    "              'IXYI': QubitOperator('X1 Y2'), 'IYXI': QubitOperator('Y1 X2')}\n",
    "\n",
    "#run QCC method\n",
    "QCC_energy, variables = QCC(qubit_H, entanglers, angle_folds, amplitude_folds, sampler,\n",
    "                            num_cycles=10, num_samples=1000, strength=1e3)\n",
    "    \n",
    "print(QCC_energy)\n",
    "print(variables)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
